package com.zzyl.serve.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;


import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理项目Service业务层处理
 *
 * @author liyi
 * @date 2025-10-11
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String NURSINGPROJECT_ALL = "nursingproject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        boolean flag = this.save(nursingProject);
        clearNursingProjectRedis();
        return flag ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        boolean flag = this.updateById(nursingProject);
        clearNursingProjectRedis();
        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        boolean flag = this.removeBatchByIds(Arrays.asList(ids));
        clearNursingProjectRedis();
        return flag ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        boolean flag = this.removeById(id);
        clearNursingProjectRedis();
        return flag ? 1 : 0;
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> all() {
        List<NursingProjectVo> nursingProjectVoList = (List<NursingProjectVo>) redisTemplate.opsForValue().get(NURSINGPROJECT_ALL);
        if (CollectionUtils.isEmpty(nursingProjectVoList)) {
            nursingProjectVoList = nursingProjectMapper.selectAll();
            redisTemplate.opsForValue().set(NURSINGPROJECT_ALL, nursingProjectVoList);
        }
        return nursingProjectVoList;
    }

    /**
     * 清理护理项目缓存
     */
    private void clearNursingProjectRedis() {
        redisTemplate.delete(NURSINGPROJECT_ALL);
    }

}
